package com.tl.commonlibrary.ui.picture;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.tl.commonlibrary.R;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.picture.bean.Picture;
import com.tl.commonlibrary.ui.picture.library.PhotoView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/6/13 0013.
 *
 * @deprecated 暂时不使用，采用代替品：  PictureSelector.create((PoundUploadActivity) context).externalPicturePreview(mDatas.indexOf(object),mDatas);
 * 图片浏览
 */

public class PictureBrowsingActivity extends BaseFragmentActivity {
    public static void start(Context context, ArrayList<Picture> pictures) {
        start(context, pictures, 0);
    }

    public static void start(Context context, Picture picture) {
        ArrayList<Picture> pictures = new ArrayList<>();
        pictures.add(picture);
        start(context, pictures);
    }

    public static void start(Context context, ArrayList<Picture> pictures, int showPosition) {
        Intent intent = new Intent(context, PictureBrowsingActivity.class);
        intent.putExtra("pictures", pictures);
        if (showPosition >= pictures.size() || showPosition < 0) {
            showPosition = 0;
        }
        intent.putExtra("showPosition", showPosition);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private ViewPager mPager;
    private ArrayList<Picture> pictures;
    private int showPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_picture_browsing);
        if (getIntent() != null) {
            pictures = (ArrayList<Picture>) getIntent().getSerializableExtra("pictures");
            showPosition = getIntent().getIntExtra("showPosition", 0);
        }
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return pictures.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = new PhotoView(PictureBrowsingActivity.this);
                view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
//                Picasso.with(container.getContext()).load(pictures.get(position).url).placeholder(R.drawable.ic_default).error(R.drawable.ic_default).into(view);
                container.addView(view);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PictureBrowsingActivity.this.onBackPressed();
                    }
                });
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mPager.setCurrentItem(showPosition);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }
}
